home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_mac.hqx / SRGP port to 5.0 (compressed) / SRGP_SPHIGS Root / MacSPHIGS / sph_traverse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-13  |  6.8 KB  |  227 lines

  1. #include "HEADERS.h"
  2. #include "sphigslocal.h"
  3. #include "sph_draw.h"
  4. #include <string.h>
  5.  
  6. #include "sph_traverse.proto.h"
  7. static void ComputeNormalTransformer(void);
  8.  
  9. /** Transforming the normal from MV to UVN
  10. Here, we compute a matrix that can be used to transform a normal 
  11. from MV to UVN.  Based on John Hughes' magic!
  12. **/
  13.  
  14. static void ComputeNormalTransformer (void)
  15. {
  16. #  define a currentMCtoUVNxform[0][0]
  17. #  define b currentMCtoUVNxform[0][1]
  18. #  define c currentMCtoUVNxform[0][2]
  19. #  define d currentMCtoUVNxform[1][0]
  20. #  define e currentMCtoUVNxform[1][1]
  21. #  define f currentMCtoUVNxform[1][2]
  22. #  define p currentMCtoUVNxform[2][0]
  23. #  define q currentMCtoUVNxform[2][1]
  24. #  define r currentMCtoUVNxform[2][2]
  25.  
  26.    bzero (currentNormalMCtoUVNxform, sizeof(matrix));
  27.    currentNormalMCtoUVNxform[0][0] = e*r-q*f;
  28.    currentNormalMCtoUVNxform[0][1] = p*f-d*r;
  29.    currentNormalMCtoUVNxform[0][2] = d*q-p*e;
  30.    currentNormalMCtoUVNxform[1][0] = q*c-b*r;
  31.    currentNormalMCtoUVNxform[1][1] = a*r-p*c;
  32.    currentNormalMCtoUVNxform[1][2] = p*b-q*a;
  33.    currentNormalMCtoUVNxform[2][0] = b*f-c*e;
  34.    currentNormalMCtoUVNxform[2][1] = d*c-a*f;
  35.    currentNormalMCtoUVNxform[2][2] = a*e-d*b;
  36. }
  37.       
  38.  
  39.  
  40. /** SPH_traverse_struct_for_display
  41. **/
  42. void
  43. SPH__traverse_struct_for_display (int structID, 
  44.                  attribute_group *inherited_attrs)
  45. {
  46.    element *curel;
  47.    matrix_4by4 global_modxform, local_modxform;
  48.    matrix_4by4 save_compositeModxform, save_MCtoUVNxform, 
  49.                save_NormalMCtoUVNxform, save_TOTALxform;
  50.    attribute_group my_attrs;
  51.  
  52.    my_attrs = *inherited_attrs;
  53.  
  54.    /* Since a subordinate structure shouldn't affect its parent's 
  55.     *   composite modeling xform, each subordinate saves and restores it.
  56.     */
  57.    MAT3copy (save_compositeModxform, currentCompositeModxform);
  58.    MAT3copy (save_MCtoUVNxform, currentMCtoUVNxform);
  59.    MAT3copy (save_NormalMCtoUVNxform, currentNormalMCtoUVNxform);
  60.    MAT3copy (save_TOTALxform, currentTOTALxform);
  61.  
  62.    /* The local matrix for any structure is initially the identity matrix,
  63.     *   so its initial composite matrix is equal to the global matrix.
  64.     */
  65.    MAT3identity (local_modxform);
  66.    MAT3copy (global_modxform, currentCompositeModxform);
  67.  
  68.  
  69. /*!*/
  70.    for /* FOR EACH ELEMENT IN THE STRUCTURE, IN ORDER: */
  71.    (curel = SPH__structureTable[structID].first_element;
  72.     curel;
  73.     curel = curel->next)
  74.  
  75.       switch (curel->type) {
  76.  
  77.        case ELTYP__POLYHEDRON:
  78.      SPH__draw_polyhedron (curel->data.poly, &my_attrs);
  79.      break;
  80.        case ELTYP__FILL_AREA:
  81.      SPH__draw_fill_area (curel, &my_attrs);
  82.      break;
  83.        case ELTYP__POLYLINE:
  84.      SPH__draw_lines (curel, &my_attrs);
  85.      break;
  86.        case ELTYP__POLYMARKER:
  87.      SPH__draw_markers (curel, &my_attrs);
  88.      break;
  89.        case ELTYP__TEXT:
  90.      SPH__draw_text
  91.         (curel->data.point, curel->info.textstring, &my_attrs);
  92.      break;
  93.  
  94.        case ELTYP__EXECUTE_STRUCTURE:
  95.      SPH__traverse_struct_for_display (curel->data.value, &my_attrs);
  96.      break;
  97.  
  98.        case ELTYP__SET_INTERIOR_COLOR:
  99.      my_attrs.interior_color = curel->data.value;
  100.      break;
  101.  
  102.        case ELTYP__SET_EDGE_COLOR:
  103.      my_attrs.edge_color = curel->data.value;
  104.      break;
  105.        case ELTYP__SET_EDGE_WIDTH:
  106.      my_attrs.edge_width = curel->data.value;
  107.      break;
  108.        case ELTYP__SET_EDGE_STYLE:
  109.      my_attrs.edge_style = curel->data.value;
  110.      break;
  111.        case ELTYP__SET_EDGE_FLAG:
  112.      my_attrs.edge_flag = curel->data.value;
  113.      break;
  114.  
  115.        case ELTYP__SET_MARKER_COLOR:
  116.      my_attrs.marker_color = curel->data.value;
  117.      break;
  118.        case ELTYP__SET_MARKER_SIZE:
  119.      my_attrs.marker_size = curel->data.value;
  120.      break;
  121.        case ELTYP__SET_MARKER_STYLE:
  122.      my_attrs.marker_style = curel->data.value;
  123.      break;
  124.  
  125.        case ELTYP__SET_TEXT_COLOR:
  126.      my_attrs.text_color = curel->data.value;
  127.      break;
  128.        case ELTYP__SET_TEXT_FONT:
  129.      my_attrs.font = curel->data.value;
  130.      break;
  131.  
  132.        case ELTYP__SET_LINE_COLOR:
  133.      my_attrs.line_color = curel->data.value;
  134.      break;
  135.        case ELTYP__SET_LINE_WIDTH:
  136.      my_attrs.line_width = curel->data.value;
  137.      break;
  138.        case ELTYP__SET_LINE_STYLE:
  139.      my_attrs.line_style = curel->data.value;
  140.      break;
  141.  
  142.        case ELTYP__CLEAR_MODXFORM:
  143.              MAT3identity (local_modxform);
  144.       MAT3copy (currentCompositeModxform, save_compositeModxform);
  145.       MAT3copy (currentTOTALxform, save_TOTALxform);
  146.       MAT3copy (currentMCtoUVNxform, save_MCtoUVNxform);
  147.       MAT3copy (currentNormalMCtoUVNxform, save_NormalMCtoUVNxform);
  148.       break;
  149.             
  150.      
  151.        case ELTYP__SET_MODXFORM:
  152.      switch (curel->info.update_type) {
  153.          case ASSIGN:
  154.            MAT3copy (local_modxform, curel->data.matrix);
  155.                break;
  156.          case POSTCONCATENATE:
  157.            MAT3mult (local_modxform, curel->data.matrix, local_modxform);
  158.            break;
  159.          case PRECONCATENATE:
  160.            MAT3mult (local_modxform, local_modxform, curel->data.matrix);
  161.            break;
  162.         }
  163.      MAT3mult (currentCompositeModxform, local_modxform, global_modxform);
  164.      MAT3mult (currentMCtoUVNxform, currentCompositeModxform,
  165.            SPH_viewTable[currentViewIndex].vo_matrix);
  166.          MAT3mult (currentTOTALxform, currentCompositeModxform, 
  167.                SPH_viewTable[currentViewIndex].cammat);
  168.      if (SPH_viewTable[currentViewIndex].rendermode > WIREFRAME)
  169.         ComputeNormalTransformer();
  170.       }
  171.  
  172.    /* The restoration referred to earlier. */
  173.    MAT3copy (currentCompositeModxform, save_compositeModxform);
  174.    MAT3copy (currentTOTALxform, save_TOTALxform);
  175.    MAT3copy (currentMCtoUVNxform, save_MCtoUVNxform);
  176.    MAT3copy (currentNormalMCtoUVNxform, save_NormalMCtoUVNxform);
  177. }
  178.       
  179.  
  180.  
  181.  
  182. #define INITIALIZED_LATER  0
  183.  
  184. static attribute_group default_attribute_group = {
  185.    INITIALIZED_LATER /* black line color */,
  186.    LINE_WIDTH_UNIT_IN_PIXELS /* line width */, CONTINUOUS /* line style */,
  187.  
  188.    INITIALIZED_LATER /* black marker color */,
  189.    MARKER_SIZE_UNIT_IN_PIXELS /* marker size */, 
  190.    MARKER_CIRCLE /* marker style */,
  191.  
  192.    INITIALIZED_LATER /* white interior color */,
  193.  
  194.    1 /* edge width */, CONTINUOUS /* edge style */,
  195.    EDGE_VISIBLE /* edge_flag */, INITIALIZED_LATER /* black edge color */,
  196.  
  197.    INITIALIZED_LATER /* black text color */, 0 /* default SRGP font */
  198. };
  199.  
  200.  
  201. void SPH__initDefaultAttributeGroup (void)
  202. {
  203.    default_attribute_group.line_color = SRGP_BLACK;
  204.    default_attribute_group.marker_color = SRGP_BLACK;
  205.    default_attribute_group.text_color = SRGP_BLACK;
  206.    default_attribute_group.edge_color = SRGP_BLACK;
  207.    default_attribute_group.interior_color = SRGP_WHITE;
  208. }
  209.  
  210.  
  211.  
  212. /** SPH_traverse_network_for_display 
  213. **/
  214. void
  215. SPH__traverse_network_for_display (view_spec *viewSpec, root_header *network)
  216. {
  217.    MAT3identity (currentCompositeModxform);
  218.    MAT3copy (currentTOTALxform, viewSpec->cammat);
  219.    MAT3copy (currentMCtoUVNxform, viewSpec->vo_matrix);
  220.    if (viewSpec->rendermode > WIREFRAME)
  221.       ComputeNormalTransformer();
  222.  
  223.    /* NOW LET'S TRAVERSE THE NETWORK!!!! */
  224.    SPH__traverse_struct_for_display
  225.       (network->root_structID, &default_attribute_group);
  226. }
  227.